﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using TexasHoldEmCroupier.UserControls;
using TexasHoldEmCroupier.Enumerators;
using System.Globalization;
using System.Drawing;

namespace TexasHoldEmCroupier.ApplicationController
{
    public partial class Controller
    {
        private void PlayerStrip_OnFold(
            object sender,
            EventArgs e)
        {
            PlayerStrip thePlayer =
                sender as PlayerStrip;

            this.AddCommentaryLine(
                MessageType.Information,
                String.Format(
                    CultureInfo.CurrentCulture,
                    "{0} folds",
                    thePlayer.PlayerName));

            thePlayer.IsAvailable = false;

            this.ActivateNextPlayer();
        }

        private void PlayerStrip_OnCheck(
            object sender,
            EventArgs e)
        {
            PlayerStrip thePlayer =
                sender as PlayerStrip;

            this.AddCommentaryLine(
                MessageType.Information,
                String.Format(
                    CultureInfo.CurrentCulture,
                    "{0} checks",
                    thePlayer.PlayerName));

            thePlayer.IsAvailable = false;

            this.ActivateNextPlayer();
        }
        
        private void PlayerStrip_OnCall(
            object sender,
            EventArgs e)
        {
            PlayerStrip thePlayer =
                sender as PlayerStrip;

            if (thePlayer.Bet.Equals(0))
            {
                this.AddCommentaryLine(
                    MessageType.Information,
                    String.Format(
                        CultureInfo.CurrentCulture,
                        "{0} calls",
                        thePlayer.PlayerName));
            }
            else
            {
                this.AddCommentaryLine(
                    MessageType.Information,
                    String.Format(
                        CultureInfo.CurrentCulture,
                        "{0} calls, betting {1:N2}",
                        thePlayer.PlayerName,
                        thePlayer.Bet));
            }

            this.Bet(thePlayer, thePlayer.Bet);

            thePlayer.IsAvailable = false;

            this.ActivateNextPlayer();
        }
        
        private void PlayerStrip_OnBet(
            object sender,
            EventArgs e)
        {
            PlayerStrip thePlayer =
                sender as PlayerStrip;

            this.AddCommentaryLine(
                MessageType.Information,
                String.Format(
                    CultureInfo.CurrentCulture,
                    "{0} bets {1:N2}",
                    thePlayer.PlayerName,
                    thePlayer.Bet));

            this.Bet(thePlayer, thePlayer.Bet);

            thePlayer.IsAvailable = false;

            foreach (PlayerStrip thisPlayer in
                this._MainView.panelPlayers.Controls)
            {
                if (!thisPlayer.Equals(thePlayer))
                {
                    if (!thisPlayer.Action.Equals(Action.Fold) &&
                        !thisPlayer.Action.Equals(Action.AllIn) &&
                        !thisPlayer.Account.Equals(0))
                    {
                        thisPlayer.IsAvailable = true;
                    }
                }
            }

            this.ActivateNextPlayer();
        }

        private void PlayerStrip_OnRaise(
             object sender,
             EventArgs e)
        {
            PlayerStrip thePlayer =
                sender as PlayerStrip;

            this.AddCommentaryLine(
                MessageType.Information,
                String.Format(
                    CultureInfo.CurrentCulture,
                    "{0} raises by {1:C2} to {2:C2}",
                    thePlayer.PlayerName,
                    thePlayer.Bet - this._MainView.CurrentBet,
                    thePlayer.Bet));

            this.Bet(thePlayer, thePlayer.Bet);

            thePlayer.IsAvailable = false;

            foreach (PlayerStrip thisPlayer in 
                this._MainView.panelPlayers.Controls)
            {
                if (!thisPlayer.Equals(thePlayer))
                {
                    if (!thisPlayer.Action.Equals(Action.Fold) &&
                        !thisPlayer.Action.Equals(Action.AllIn) &&
                        !thisPlayer.Account.Equals(0))
                    {
                        thisPlayer.IsAvailable = true;
                    }
                }
            }

            this.ActivateNextPlayer();
        }

        private void PlayerStrip_OnAllIn(
             object sender,
             EventArgs e)
        {
            PlayerStrip thePlayer =
                sender as PlayerStrip;

            this.AddCommentaryLine(
                MessageType.Information,
                String.Format(
                    CultureInfo.CurrentCulture,
                    "{0} goes all in, betting {1:C2}",
                    thePlayer.PlayerName,
                    thePlayer.Account));

            this.Bet(thePlayer, thePlayer.Account);

            thePlayer.IsAvailable = false;

            foreach (PlayerStrip thisPlayer in
                this._MainView.panelPlayers.Controls)
            {
                if (!thisPlayer.Equals(thePlayer))
                {
                    if (!thisPlayer.Action.Equals(Action.Fold) &&
                        !thisPlayer.Action.Equals(Action.AllIn) &&
                        !thisPlayer.Account.Equals(0))
                    {
                        thisPlayer.IsAvailable = true;
                    }
                }
            }

            this.ActivateNextPlayer();
        }
    }
}
